<template>
  <lay-container fluid="true" style="padding: 10px">
    <lay-card>
      <lay-row>
        <lay-col :md="24">
          <lay-col :md="6">
            <lay-button size="sm" type="danger" @click="clear()" v-permission="PerKey.Log_Clear" prefix-icon="layui-icon-delete">清空登录日志</lay-button>
              <lay-button size="sm" type="normal" @click="exportExcel()" prefix-icon="layui-icon-export">导出EXCEL</lay-button>
        </lay-col>
          <lay-col :md="18">
          <lay-form :model="queryModel" ref="layFormRef">
            <lay-row>
              <lay-col :md="4">
                <lay-input size="sm" v-model="queryModel.nickName" style="width: 90%" placeholder="操作人"></lay-input>
              </lay-col>
              <lay-col :md="10" :lg="8">
                <lay-form-item label="时间范围" prop="rangeTime">
                  <lay-date-picker size="sm" v-model="rangeTime" range style="width: 95%" :placeholder="['开始日期', '结束日期']"></lay-date-picker>
                </lay-form-item>
              </lay-col>
              <lay-col :md="4">
                <lay-form-item label-width="0">
                  <lay-button size="sm" type="primary" @click="GetList(queryModel)" prefix-icon="layui-icon-search">查询</lay-button>
                  <lay-button size="sm" @click="reset">重置</lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-col>
        </lay-col>
        <lay-col :md="24">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource"
            v-model:selectedKeys="selectedKeys" @change="change" :size="tablesize">
            <template v-slot:user="{ data }">
              {{ data.deptName + " - " + data.nickName }}
            </template>
          </lay-table>
        </lay-col>
      </lay-row>
    </lay-card>
  </lay-container>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs } from "vue";
import { layer } from "@layui/layer-vue";
import { getListLogin, clearlogLogin } from "../../../api/module/systemlog";
import { PerKey } from "../../../library/permissionConst"
import Uitl from '../../../library/Util'
const selectedKeys = ref(["1"]);
const page = ref({ total: 0, limit: 15, current: 1, showCount: true, showRefresh: true, limits: [10, 15, 20, 50, 100, 200] });
const tablesize = ref('md');
const columns = [
  {
    title: "Id",
    width: "80px",
    key: "id"
  }, {
    title: "登录人",
    width: "500px",
    customSlot: "user"
  }, {
    title: "Ip",
    width: "200px",
    key: "ip"
  },
  {
    title: "时间",
    key: "time"
  }
]
const dataSource = ref([])
const rangeTime = ref([])

const putData = reactive({
  queryModel: {
    pageNum: page.value.current,
    pageSize: page.value.limit,
    startDate: "",
    endDate: "",
    nickName: "",
    exportExcel: false
  }
})

const { queryModel } = toRefs(putData)

const layFormRef = ref();
const reset = function () {
  rangeTime.value = []
  layFormRef.value.reset()
}


const GetList = function (query: any) {

  if (query != null) {
    queryModel.value.startDate = rangeTime.value[0]
    queryModel.value.endDate = rangeTime.value[1]
    queryModel.value.nickName = query.nickName
  }

  getListLogin(queryModel.value).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
      page.value.total = res.data.count
    }
  })
}

const change = function ({ current, limit }: any) {
  queryModel.value.pageNum = current
  queryModel.value.pageSize = limit
  GetList(queryModel.value);
};

const exportExcel = () => {
  layer.confirm("确认导出所有登录日志", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        queryModel.value.exportExcel = true
        getListLogin(queryModel.value).then((res) => {
          const { filePath, fileName } = res.data
          if (res.status == 1) {
            Uitl.downloadFileForUrl(filePath, fileName)
            layer.close(layerId);
          } else {
            layer.msg('导出失败');
          }
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

const clear = function () {
  layer.confirm("确认清空登录日志", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        clearlogLogin().then((res) => {
          if (res.status == 1) {
            GetList(queryModel.value)
            layer.msg(res.msg);
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}


GetList(queryModel.value)


</script>


<style scoped>

</style>
